package com.ruoyi.web.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC59g4sQDZLhW/TuDx/xALT9pqmX7BpVqbQ+32l3C73922lLoWnrWfNwhRqEZOAhqaSoo2pOQrabHVwv2y5zcc00IpbZSck/aRC7Dz04g0zvkFD0QtsCzTpwTky3p9AFhsQDRTnF+Uinb28EexL8K7spspe87ZRIzleRZdJ+XbRBrQSX7HJ3JsP2KAE6c2Vdf9P15d1aqLwrUHjEvuWyWknCAnhmLxBfUldEroNHJmgjlnlHKP1mAptCxHEr5y4f4HulMexiX1Cz6L4snHaHL9/BLf0Bp0mCGBUa3rCaaP0zDY/aabaN5Z0/WPnuIjyIY2NyMa2R8HkweS7hTZiC+gFAgMBAAECggEAXrFo7ieUfkhodUsvo2/QcNb2URoQ3tL9BxXJGZhRYntgHejerk+iuCfH9WsVzTDDyY90y2L8n9TyyILxfnvGoKJAkeGkWTW/W0SolZo2Vs7etQXR5yadvxphYgHy3dNj3HyM1FBaoZHpd8c4qELbEC6uhkNb510kbVjKfSTG2BDPg9AKAyDLHa4bqiKoqobaXzNg1eMGkKdPTSUZbnCJgzrO7ekcT16yjKk9HHPyEBmcjduvwyWaymsoDuE/aAvAvqp92rnTipmufNM7sgPWMDAW5eXIPQ8K86ciAsaMBFQdswjszZxFqEE8sCaGoPBsk66tii5lG9FmOhQxEj8MgQKBgQDfX2cGNdbW6/B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whSczKEFGgL+oyb9OwZuCyLj0XSM2XqWsGd18TmwqksUnodxtAY7HwnIN0Q5BbJ1UamaSWJiWi//pGUsxsXDXbhmuNVc5+n57iHUd+IyAtcfFFwXnM1kv3hlM+lMypDjyQDJ+zGSE9gTzcy3BygbfjHJ56iBzvVWsDaJr4+WuujfUwIDAQAB";

    //生成支付订单接口
    public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000122683497");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney().toString());
        model.setSubject(payData.getName());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
//        request.setNotifyUrl("http://localhost:8888/#/list");

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000122683497");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();

    }
}
